package com.chinatgl.latte.ec.main.index._1;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chinatgl.latte.ec.R;
import com.chinatgl.latte.ec.R2;
import com.chinatgl.latte.ec.mode.AD;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

public class RecyclerViewHFAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 0;

    private static final int TYPE_FOOTER = 1;

    private static final int TYPE_ITEM = 2;

    private static final int TYPE_EMPTY = 3;

    private View mHeaderView;

    private View mFooterView;

    private View mEmptyView;

    private List<AD> items;
    private Context context;

    public RecyclerViewHFAdapter(Context context, List<AD> data) {
        this.context = context;
        this.items = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_list_home_home, parent, false);
            return new ItemHolder(v);
        } else if (viewType == TYPE_HEADER) {
            View v = mHeaderView;
            return new HeaderHolder(v);
        } else if (viewType == TYPE_FOOTER) {
            View v = mFooterView;
            return new FooterHolder(v);
        } else if (viewType == TYPE_EMPTY) {
            View v = mEmptyView;
            return new EmptyHolder(v);
        }
        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        if (holder instanceof ItemHolder) {
            final AD dataItem = getItem(position);


            ImageView iv_home_list = (ImageView) ((ItemHolder) holder).ll.findViewById(R.id.iv_home_list);




            Glide.with(context)
                    .load(dataItem.getImage() + "?imageView2/q/50")
                    .placeholder(R.mipmap.img_zhanwei_xianshi)
                    .error(R.mipmap.img_zhanwei_xianshi)
                    .into(iv_home_list);

            iv_home_list.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String type = dataItem.getType();
                    Intent intent;
                    switch (type) {

                        case "0"://图片


                            break;
                        case "1"://单品
                            //商品的id

                            break;
                        case "2"://专题


                            break;
                        case "3"://网页


                            break;
                    }
                }
            });

//               iv_home_list.setImageResource(R.mipmap.img_zhanwei_xianshi);

            TextView tv_title_home_list = (TextView) ((ItemHolder) holder).ll.findViewById(R2.id.tv_title_home_list);

            TextView tv_title2_home_list = (TextView) ((ItemHolder) holder).ll.findViewById(R2.id.tv_title2_home_list);
            tv_title_home_list.setText(dataItem.getTitle());
            tv_title2_home_list.setText(dataItem.getDesc());
        } else if (holder instanceof HeaderHolder) {

        } else if (holder instanceof FooterHolder) {

        } else if (holder instanceof EmptyHolder) {

        }
    }

    @Override
    public int getItemCount() {

        int count;
        int size = items.size();
        if (size == 0 && null != mEmptyView) {
            count = 1;
        } else {
            count = getHeadViewSize() + size + getFooterViewSize();
        }
        return count;

    }

    @Override
    public int getItemViewType(int position) {
        int size = items.size();
        if (size == 0 && null != mEmptyView) {
            return TYPE_EMPTY;
        } else if (position < getHeadViewSize()) {
            return TYPE_HEADER;
        } else if (position >= getHeadViewSize() + items.size()) {
            return TYPE_FOOTER;
        }
        return TYPE_ITEM;
    }

    private int getHeadViewSize() {
        return mHeaderView == null ? 0 : 1;
    }

    private int getFooterViewSize() {
        return mFooterView == null ? 0 : 1;
    }

    private AD getItem(int position) {
        return items.get(position - getHeadViewSize());
    }


    //add a header to the adapter
    public void addHeader(View header) {
        mHeaderView = header;
        notifyItemInserted(0);
    }

    //remove a header from the adapter
    public void removeHeader(View header) {
        notifyItemRemoved(0);
        mHeaderView = null;
    }

    //add a footer to the adapter
    public void addFooter(View footer) {
        mFooterView = footer;
        notifyItemInserted(getHeadViewSize() + items.size());
    }

    //remove a footer from the adapter
    public void removeFooter(View footer) {
        notifyItemRemoved(getHeadViewSize() + items.size());
        mFooterView = null;
    }

    //add data
    public void addDatas(List<AD> data) {
        items.addAll(data);
        notifyItemInserted(getHeadViewSize() + items.size() - 1);
    }

    //add data
    public void addData(AD data) {
        items.add(data);
        notifyItemInserted(getHeadViewSize() + items.size() - 1);
    }

    //refresh data
    public void refreshData(List<AD> datas) {
        items.clear();
        addDatas(datas);
    }

    public void setEmptyView(View emptyView) {
        mEmptyView = emptyView;
        notifyItemInserted(0);
    }


    class ItemHolder extends RecyclerView.ViewHolder {
        // TODO: 2017/4/30
        //  item_list_home_home
        LinearLayout ll;

        public ItemHolder(View itemView) {
            super(itemView);
            ll = (LinearLayout) itemView;
        }
    }

    class HeaderHolder extends RecyclerView.ViewHolder {

        public HeaderHolder(View itemView) {
            super(itemView);
        }
    }

    class FooterHolder extends RecyclerView.ViewHolder {
        public FooterHolder(View itemView) {
            super(itemView);
        }
    }

    class EmptyHolder extends RecyclerView.ViewHolder {
        public EmptyHolder(View itemView) {
            super(itemView);
        }
    }
}
